#!/usr/bin/env bash

hive -e "
alter table bdm.mmall_order add if not exists partition (day='2019-06-25');
alter table bdm.mmall_order_item add if not exists partition (day='2019-06-25');
alter table bdm.mmall_shipping add if not exists partition (day='2019-06-25');
alter table fdm.fdm_order_info add if not exists partition (day='2019-06-25');

insert overwrite table fdm.fdm_order_info partition(day='2019-06-25')
    select
        o.id int,
        o.order_no,
        o.user_id,
        o.shipping_id,
        o.payment,
        o.payment_type,
        o.postage,
        o.status,
        o.payment_time,
        o.send_time,
        o.end_time,
        o.close_time,
        o.create_time,
        o.update_time,

        oi.product_id,
        oi.product_name,
        oi.product_image,
        oi.current_unit_price,
        oi.quantity,
        oi.total_price,

        s.receiver_name,
        s.receiver_phone,
        s.receiver_mobile,
        s.receiver_province,
        s.receiver_city,
        s.receiver_district,
        s.receiver_address,
        s.receiver_zip

    from bdm.mmall_order as o
        left join bdm.mmall_order_item as oi
    on (o.order_no = oi.order_no)
        left join bdm.mmall_shipping as s
    on (o.user_id = oi.user_id)
    where o.day='2019-06-25';
    "